<?php
/**
 * Created by PhpStorm.
 * User: 王飞
 * Date: 2017/9/2
 * Time: 12:17
 */
namespace app\admin\controller\order;

use app\admin\model\StaffCard;
use app\common\controller\Backend;
use app\common\model\Category as CategoryModel;

/**
 * Class Orderlist
 * @package app\admin\controller\staff
 * 员工列表
 */
class Stafflist extends Backend
{
	protected $model = null;
	protected $serverdayslist=null;
	
	public function _initialize()
	{
		parent::_initialize();
		$this->model = model('Staff');
	}
	public function index()
	{
		/**
		 * 此函数查看员工的列表
		 */
		$order_id=$_GET['orderid'];
		
		$this->view->assign('orderid',$_GET['orderid']);
		if ($this->request->isAjax()) {
			$orderflg=db('order')->where('id',$order_id)->find();
			list($where, $sort, $order, $offset, $limit) = $this->buildparams(NULL);
			$total = $this->model->alias('a')
				->where($where)->where('type',$orderflg['staff_type_id'])
				->where(function($query) use ($orderflg){
					$query->table('fa_worklog') ->where('begintime','<= time',$orderflg['begintime'])
						->where('endtime','> time',$orderflg['begintime'])
						->whereor('begintime',['>=',$orderflg['begintime']],['<',$orderflg['endtime']],'and')
						->whereor('endtime',['>',$orderflg['begintime']],['<',$orderflg['endtime']],'and');
				//	->whereor('endtime','between','a.begintime,a.endtime')->whereor('begintime','between','a.begintime,a.endtime');
				},'not exists')
				->where(function($query)use ($orderflg){
					$query->table('fa_worklog') ->where('order_id',$orderflg['id'])->where('staff_id=a.id');
				},'not exists')
				->order($sort, $order)
				->count();
			$list = $this->model->alias('a')
				->where($where)->where('type',$orderflg['staff_type_id'])
				->where(function($query) use ($orderflg){
					$query->table('fa_worklog') ->where('begintime','<= time',$orderflg['begintime'])
						->where('endtime','> time',$orderflg['begintime'])
						->whereor('begintime',['>=',$orderflg['begintime']],['<',$orderflg['endtime']],'and')
						->whereor('endtime',['>',$orderflg['begintime']],['<',$orderflg['endtime']],'and');
					//	->whereor('endtime','between','a.begintime,a.endtime')->whereor('begintime','between','a.begintime,a.endtime');
				},'not exists')
				->where(function($query) use ($orderflg){
					$query->table('fa_worklog') ->where('order_id',$orderflg['id'])->where('staff_id=a.id');
				},'not exists')
				->order($sort, $order)
				->limit($offset, $limit)
				->select();
			$result = array("total" => $total, "rows" => $list);
			return json($result);
		}
		return $this->view->fetch();
	}
}